/**
 * Copyright (c) 2003-2017 The Apereo Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://opensource.org/licenses/ecl2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sakaiproject.gradebookng.tool.pages;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.sakaiproject.gradebookng.business.GbRole;
import org.sakaiproject.gradebookng.business.util.EventHelper;
import org.sakaiproject.gradebookng.tool.panels.StudentGradeSummaryGradesPanel;
import org.sakaiproject.portal.util.PortalUtils;
import org.sakaiproject.rubrics.api.RubricsConstants;
import org.sakaiproject.user.api.User;

/**
 *
 * The page that students get. Similar to the student grade summary panel that instructors see.
 *
 * @author Steve Swinsburg (steve.swinsburg@gmail.com)
 *
 */
public class StudentPage extends BasePage {

	private static final long serialVersionUID = 1L;

	private String gradebookUid;
	private String siteId;

	public StudentPage() {

		if (role == GbRole.NONE) {
			sendToAccessDeniedPage(getString("error.role"));
		}

		gradebookUid = getCurrentGradebookUid();
		siteId = getCurrentSiteId();

		final User u = getCurrentUser();

		final Map<String, Object> userData = new HashMap<>();
		userData.put("studentUuid", u.getId());
		userData.put("groupedByCategoryByDefault", true);

		add(new Label("heading", new StringResourceModel("heading.studentpage").setParameters(u.getDisplayName())));
		StudentGradeSummaryGradesPanel sgsgp = new StudentGradeSummaryGradesPanel("summary", Model.ofMap(userData));
		add(sgsgp);

		EventHelper.postStudentViewEvent(this.businessService.getGradebook(gradebookUid, siteId), u.getId());
	}

	@Override
	public void renderHead(final IHeaderResponse response) {
		super.renderHead(response);

		final String version = PortalUtils.getCDNQuery();

		// tablesorted used by student grade summary
		response.render(JavaScriptHeaderItem.forScript("includeWebjarLibrary('jquery.tablesorter')", null));
		response.render(JavaScriptHeaderItem.forScript("includeWebjarLibrary('jquery.tablesorter/2.27.7/dist/css/theme.bootstrap.min.css')", null));

		// GradebookNG Grade specific styles and behaviour
		response.render(
				JavaScriptHeaderItem.forUrl(
						String.format("/gradebookng-tool/scripts/gradebook-grade-summary.js%s", version)));
	}
}
